/********************************************************************************
* File Name:	stuaddlist.cpp
* Description:	第10章实例训练          
* Reference book:《Linux环境下Qt4图形界面与MySQL编程》，机械工业出版社.2012.1  
* E_mail: openlinux2011@gmail.com		                
*
********************************************************************************/
#include "stuaddlist.h"
#include <QSqlRecord>
#include <QSqlDatabase>
#include <QSqlQuery>

extern QString snumber; //用于保存登录学生的用户名

//定菜界面构造函数
StuAddList::StuAddList(QWidget* parent) : QDialog(parent)
{
        setupUi(this);

        /*设置定菜界面*/
        tbAddOrder->setText(tr("<<"));
        tbAddOrder->setIcon(QPixmap("./images/4.png")); //设置图标
        tbAddOrder->setIconSize(QPixmap("./images/4.png").size()); //设置大小
        tbAddOrder->setAutoRaise(TRUE); //鼠标离开，自动弹
        tbAddOrder->setToolButtonStyle(Qt::ToolButtonTextUnderIcon); //方式，文字显示在图片下边
        tbDeleteOrder->setText(tr(">>"));
        tbDeleteOrder->setIcon(QPixmap("./images/end.png")); //设置图标
        tbDeleteOrder->setIconSize(QPixmap("./images/end.png").size()); //设置大小
        tbDeleteOrder->setAutoRaise(TRUE); //鼠标离开，自动弹
        tbDeleteOrder->setToolButtonStyle(Qt::ToolButtonTextUnderIcon); //方式，文字显示在图片下边

        /*信号与槽函数的连接*/
        connect(tbAddOrder, SIGNAL(clicked()),this,SLOT(stuAddOrder_clicked())); //添加定菜
        connect(tbDeleteOrder, SIGNAL(clicked()),this,SLOT(stuDeleteOrder_clicked())); //删除定菜
        connect(submitOrder,SIGNAL(clicked()),this,SLOT(stuSubmitOrder_clicked())); //提交定菜并付款
}

void StuAddList::StuAddList_load()//界面加载控制菜单信息及已点菜信息显示
{
    addListMenuModel = new QSqlTableModel(this);
    addListMenuModel->setTable("menulist"); //连接表menulist
    addListMenuModel->setEditStrategy(QSqlTableModel::OnManualSubmit); //OnManualSubmit表明我们要提交修改才能使其生效
    addListMenuModel->select(); //执行select()函数能够将最新的信息显示在视图中
    tvAddListMenu->setModel(addListMenuModel); //将表格与视图绑定

    andListOrderModel = new QSqlRelationalTableModel(this);
    andListOrderModel->setTable("orderMenu"); //连接表orderMenu
    andListOrderModel->setEditStrategy(QSqlRelationalTableModel::OnManualSubmit); //OnManualSubmit表明我们要提交修改才能使其生效
    andListOrderModel->setRelation(1,QSqlRelation("menulist","mid","mname")); //通过mid将mnamne与orderMenu中的信息绑定
    andListOrderModel->setFilter("snumber = '"+snumber+"'"); //根据姓名进行筛选
    andListOrderModel->removeColumn(2); //不显示snumber
    andListOrderModel->select(); //执行select()函数能够将最新的信息显示在视图中
    tvAddListOrder->setModel(andListOrderModel); //将表与视图绑定
}

void StuAddList::stuAddOrder_clicked() //点菜
{
    int curRow = tvAddListMenu->currentIndex().row(); //获取视图当前被选中行的行号
    QSqlQuery queryMenu;
    QString mid,insertStr;
    mid=addListMenuModel->record(curRow).value("mid").toString(); //获取选中行对应的mid的值
    insertStr="insert into orderMenu(mid,snumber) values('"+mid+"','"+snumber+"')";
    queryMenu.exec(insertStr); //将点菜信息插入orderMenu表中
    andListOrderModel->select(); //执行select()函数能够将最新的信息显示在视图中
}

void StuAddList::stuDeleteOrder_clicked() //删除已点的菜
{
    int curRow = tvAddListOrder->currentIndex().row();//获取选中行的行号
    andListOrderModel->removeRow(curRow); //删除该行
    int ok = QMessageBox::warning(this,tr("删除当前行!"),tr("你确定删除当前行吗？"),QMessageBox::Yes,QMessageBox::No);
    if(ok == QMessageBox::No)
    {
       andListOrderModel->revertAll(); //如果不删除，则撤销
    }
    else
       andListOrderModel->submitAll(); //否则提交，在数据库中删除该行
}

void StuAddList::stuSubmitOrder_clicked() //提交点菜，付款
{
    int rowNum=andListOrderModel->rowCount(); //获取点菜的个数
    int sum=0;
    QString oid,mid,selectStr,scardNumber;
    QSqlQuery query;
    for(int i=0;i<rowNum;i++) //计算点菜所需费用
    {
        oid=andListOrderModel->record(i).value("oid").toString(); //获取当前行的oid值
        selectStr="select mid from orderMenu where oid="+oid+"";
        query.exec(selectStr); //选择oid对应的mid从orderMenu表中
        if(query.next())
        {
            mid=query.value(0).toString();
            selectStr="select mprice from menulist where mid="+mid+""; //获取mid对应的mprice从menulist表中
            query.exec(selectStr);
            if(query.next())
            {
                sum += (query.value(0).toInt()); //累加开销
                selectStr="delete from orderMenu where oid="+oid+""; //将计算完的信息从orderMenu表中删除
                query.exec(selectStr);
            }
        }
    }

    selectStr="insert into dealRecord(snumber,sum) values('"+snumber+"',"+tr("%1").arg(sum)+")";
    if(query.exec(selectStr)) //记录饭店营业信息
    {
        selectStr="select scardNumber from Student where snumber='"+snumber+"'";
        query.exec(selectStr); //获取用户的卡号
        if(query.next())
        {
            scardNumber=query.value(0).toString();
            selectStr="select money from card where scardNumber='"+scardNumber+"'";
            query.exec(selectStr); //获取用户卡中剩余金额
            if(query.next())
            {
                sum =query.value(0).toInt()-sum;
                selectStr="update card set money="+tr("%1").arg(sum)+" where scardNumber='"+scardNumber+"'";
                if(query.exec(selectStr)) //扣除本次点菜开销
                {
                    QMessageBox::warning(this , tr("付款提示"),tr("付款成功，您点的菜正在准备！"));
                }
            }
        }
    }
}
